package edu.ayit.controller.admin;

import edu.ayit.pojo.Type;
import edu.ayit.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.persistence.Id;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private TypeService typeService;

    //分页显示
    @GetMapping("/types")
    //size：每页显示的条数，sort：根据什么排序，direction：指定排序的方向
    public String types(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                        Model model){

        Page<Type> page = typeService.listType(pageable);
        model.addAttribute("page",page);
        return "admin/types";
    }

    //跳转到新增分类页面
    @GetMapping("/types/input")
    public String input(Model model){
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    //点击编辑按钮，跳转到修改页面（与增加分类页面共用），并带上需要编辑的值
    @GetMapping("/types/{id}/input")
    //这里的@PathVariable是为了获取路径中的对应参数
    public String input(@PathVariable Long id, Model model){
        Type type = typeService.getType(id);
        model.addAttribute("type",type);
        return "admin/types-input";
    }

    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id){
        typeService.deleteType(id);
        return "redirect:/admin/types";
    }

    //新增分类
    @PostMapping("/types")
    public String add(Type type, HttpSession session){
        Type type1 = typeService.getTypeByName(type.getName());
        if(type==null){
            session.setAttribute("type_add_msg","操作失败");
        }else
            if(type1!=null){//查询该分类是否已存在
                session.setAttribute("type_add_msg_exist","该分类已存在");
                return "redirect:/admin/types/input";
        }
        else{
                session.setAttribute("type_add_msg","操作成功");
        }
        typeService.saveType(type);
        return "redirect:/admin/types";
    }
    @PostMapping("/types/{id}/update")
    public String update(Type type, HttpSession session, @PathVariable Long id){
        System.out.println("===========================");
        Type type1 = typeService.getTypeByName(type.getName());
        if(type==null){
            session.setAttribute("type_add_msg","更新失败");
        }else
        if(type1!=null){//查询该分类是否已存在
            session.setAttribute("type_add_msg_exist","该分类已存在");
            return "redirect:/admin/types";
        }
        else{
            session.setAttribute("type_add_msg","操作成功");
        }
        typeService.updateType(id,type);
        return "redirect:/admin/types";
    }
}
